const express = require('express');
const router = express.Router();
const announcementController = require('../controllers/announcementController');
const { authenticateToken, authenticateAdmin } = require('../middleware/auth');
const { adminLogger } = require('../middleware/adminLogger');

// ===== 用户端接口（需要认证）=====
router.get('/public', authenticateToken, announcementController.getPublicAnnouncements);
router.get('/public/:id', authenticateToken, announcementController.getAnnouncementDetail);
router.get('/home', announcementController.getHomeAnnouncements); // 首页公告不需要认证

// ===== 管理员端接口（需要管理员权限）=====
router.get(
  '/admin/all',
  authenticateAdmin,
  announcementController.getAllAnnouncements
);

router.post(
  '/admin',
  authenticateAdmin,
  adminLogger('create_announcement', (req) => `创建公告: ${req.body.title}`),
  announcementController.createAnnouncement
);

router.put(
  '/admin/:id',
  authenticateAdmin,
  adminLogger('update_announcement', (req) => `更新公告ID: ${req.params.id}`),
  announcementController.updateAnnouncement
);

router.delete(
  '/admin/:id',
  authenticateAdmin,
  adminLogger('delete_announcement', (req) => `删除公告ID: ${req.params.id}`),
  announcementController.deleteAnnouncement
);

router.post(
  '/admin/:id/publish',
  authenticateAdmin,
  adminLogger('publish_announcement', (req) => `发布公告ID: ${req.params.id}`),
  announcementController.publishAnnouncement
);

router.post(
  '/admin/:id/archive',
  authenticateAdmin,
  adminLogger('archive_announcement', (req) => `归档公告ID: ${req.params.id}`),
  announcementController.archiveAnnouncement
);

router.get(
  '/admin/stats',
  authenticateAdmin,
  announcementController.getAnnouncementStats
);

module.exports = router;

